<?php

$data = '"LINE1","LINE2","LINE3","LINE4","LINE5","APPELASHN","SUBS_NUM","SURNAME","DATE"' . "\n";

$prev_subscriber_id = "";

foreach ($subscribers->result() as $subscriber)
{
	if ($subscriber->subscriber_id != $prev_subscriber_id)
	{
		if ($prev_subscriber_id)
			$data .= "\n";
			
		$data .=	'"' . $subscriber->title . ' ' . $subscriber->initials . ' ' . $subscriber->last_name . '",' .	// Line 1 (Title Initials Last_Name)
					'"' . $subscriber->address1 . '",' .															// Line 2 (Address1)
					'"' . $subscriber->suburb .', ' . $subscriber->post_code . '",' .																// Line 3 (Suburb)
					'"' . $subscriber->address2 . '",' .															// Line 4 (Address2)
					'"' . '",' .																					// Line 5
					'"' . $subscriber->title . ' ' . $subscriber->last_name . '",' .								// Line 6 (Title Last_Name)
					'"' . $subscriber->subscriber_id . '",' .														// Line 7 (Subscriber_ID)
					'"' . $subscriber->last_name . '",' .															// Line 8 (Last_Name)
					'"' . $subscriber->expiry_date . '"';															// Line 9 (Expiry_Date)
					
		$prev_subscriber_id = $subscriber->subscriber_id;
	}
}

force_download('in_arrears_mail_merge.csv', $data); 
?>